package net.soevar.action.master;

import java.math.BigDecimal;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import net.soevar.common.Common;
import net.soevar.common.CommonPrivilages;
import net.soevar.database.dao.BeritaDao;
import net.soevar.database.dao.DaoFactory;
import net.soevar.database.model.Berita;
import net.soevar.database.model.JenisBerita;
import net.soevar.database.model.Status;

import org.hibernate.Session;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;
import org.zkforge.ckez.CKeditor;
import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.event.Event;
import org.zkoss.zk.ui.event.EventListener;
import org.zkoss.zk.ui.util.GenericAutowireComposer;
import org.zkoss.zul.Borderlayout;
import org.zkoss.zul.Caption;
import org.zkoss.zul.Center;
import org.zkoss.zul.Checkbox;
import org.zkoss.zul.Column;
import org.zkoss.zul.Columns;
import org.zkoss.zul.Combobox;
import org.zkoss.zul.Comboitem;
import org.zkoss.zul.Decimalbox;
import org.zkoss.zul.Grid;
import org.zkoss.zul.Hbox;
import org.zkoss.zul.Label;
import org.zkoss.zul.ListModel;
import org.zkoss.zul.Messagebox;
import org.zkoss.zul.Row;
import org.zkoss.zul.RowRenderer;
import org.zkoss.zul.Rows;
import org.zkoss.zul.SimpleListModel;
import org.zkoss.zul.South;
import org.zkoss.zul.Textbox;
import org.zkoss.zul.Toolbar;
import org.zkoss.zul.Toolbarbutton;
import org.zkoss.zul.Window;

public class BeritaAction extends GenericAutowireComposer {
	private static final long serialVersionUID = 1581074810644395505L;
	private Window addWindow;
	private Grid grid;
	private Textbox searchjudul;
	private Combobox searchstatus;
	private Combobox searchjenisberita;

	private Berita berita;
	private Toolbarbutton add;
	private boolean edit = false;
	private boolean delete = false;

	private Textbox judul;
	private Combobox jenisBerita;
	private CKeditor isi;
	private Combobox status;

	@Override
	public void doAfterCompose(Component comp) throws Exception {
		super.doAfterCompose(comp);
		if (session.getAttribute("usersTemp") == null
				|| !CommonPrivilages.checkPrevilages(CommonPrivilages.READ)) {
			session.removeAttribute("usersTemp");
			execution.sendRedirect("/logoff");
			return;
		}
		if (session.getAttribute("usersTemp") == null) {
			session.removeAttribute("usersTemp");
			execution.sendRedirect("/logoff");
		}
		add.setVisible(CommonPrivilages
				.checkPrevilages(CommonPrivilages.CREATE));
		add.setTooltiptext("Tambah");
		edit = CommonPrivilages.checkPrevilages(CommonPrivilages.UPDATE);
		delete = CommonPrivilages.checkPrevilages(CommonPrivilages.DELETE);

		Common.insertCombo(searchstatus, "nama", Status.class);
		Common.insertCombo(searchjenisberita, "nama", JenisBerita.class);

		jenisBerita = new Combobox();
		Common.insertCombo(jenisBerita, "nama", JenisBerita.class);
		jenisBerita.setReadonly(true);

		status = new Combobox();
		Common.insertCombo(status, "nama", Status.class);
		status.setReadonly(true);
		// onSearchDefault(null);
	}

	class BeritaRenderer implements RowRenderer {

		@Override
		public void render(final Row arg0, Object arg1) throws Exception {
			// TODO Auto-generated method stub
			final Berita berita = (Berita) arg1;
			new Label(berita.getJenisBerita().getNama()).setParent(arg0);
			new Label(berita.getJudul()).setParent(arg0);
			new Label(berita.getStatus().getNama()).setParent(arg0);

			Hbox toolbar = new Hbox();
			Toolbarbutton button = new Toolbarbutton("",
					"/include/img/icon-edit.png");
			button.setTooltip("Rubah Data");
			button.setVisible(edit);
			button.addEventListener("onClick", new EventListener() {

				@Override
				public void onEvent(Event arg0) throws Exception {
					init(berita);
					addWindow.setVisible(true);
					addWindow.onModal();

				}
			});
			button.setParent(toolbar);

			button = new Toolbarbutton("", "/include/img/icon-delete.png");
			button.setTooltiptext("Hapus Data");
			button.setVisible(delete);
			button.addEventListener("onClick", new EventListener() {
				@Override
				public void onEvent(Event event) throws Exception {
					Messagebox.show("Apakah yakin ingin menghapus data ini ?",
							"Question", Messagebox.OK | Messagebox.CANCEL,
							Messagebox.QUESTION, new EventListener() {

								@Override
								public void onEvent(Event event)
										throws Exception {
									int i = new Integer(event.getData()
											.toString());
									if (i == Messagebox.OK) {
										try {
											BeritaDao beritaDao = DaoFactory
													.getInstance()
													.getBeritaDao();
											beritaDao.delete(beritaDao
													.merge(berita));
											arg0.detach();
										} catch (Exception e) {
											e.printStackTrace();
											Messagebox
													.show("Data ini tidak dapat dihapus ..");
										}

									}

								}
							});

				}
			});
			button.setParent(toolbar);

			toolbar.setParent(arg0);
		}
	}

	private void init(Berita berita) {
		this.berita = berita;
		Common.clear(addWindow);
		Borderlayout borderlayout = new Borderlayout();
		Center center = new Center();
		center.setParent(borderlayout);
		center.setFlex(true);

		Grid grid = new Grid();
		grid.setParent(center);
		grid.setWidth("100%");
		grid.setHeight("100%");
		grid.setOddRowSclass("non-odd");

		Columns columns = new Columns();
		columns.setParent(grid);

		Column column = new Column();
		column.setParent(columns);
		column.setWidth("10%");
		column = new Column();
		column.setParent(columns);

		Rows rows = new Rows();
		rows.setParent(grid);

		Row row = new Row();
		row.setStyle("border:0px;background: transparent;");
		row.setParent(rows);
		row.appendChild(new Label("Judul"));
		row.appendChild(judul = new Textbox(berita.getJudul() == null ? ""
				: berita.getJudul()));
		judul.setCols(80);

		row = new Row();
		row.setStyle("border:0px;background: transparent;");
		row.setParent(rows);
		row.appendChild(new Label("Jenis Berita"));
		Common.selectComboItem(jenisBerita,
				berita.getJenisBerita() == null ? "" : berita.getJenisBerita()
						.getNama());
		row.appendChild(jenisBerita);
		jenisBerita.setCols(40);

		row = new Row();
		row.setStyle("border:0px;background: transparent;");
		row.setParent(rows);
		row.appendChild(new Label("Isi"));
		row.appendChild(isi = new CKeditor());
		isi.setCustomConfigurationsPath("/include/js/ckeditor_config.js");
		isi.setFilebrowserImageBrowseUrl("media");
		isi.setFilebrowserImageUploadUrl("media");
		isi.setValue(berita.getIsi());
		isi.setWidth("90%");
		isi.setHeight("200px");

		row = new Row();
		row.setStyle("border:0px;background: transparent;");
		row.setParent(rows);
		row.appendChild(new Label("Status"));
		Common.selectComboItem(status, berita.getStatus() == null ? "" : berita
				.getStatus().getNama());
		row.appendChild(status);
		status.setCols(40);

		South south = new South();
		south.setFlex(true);
		south.setParent(borderlayout);

		Toolbar toolbar = new Toolbar();
		toolbar.setHeight("30px");
		toolbar.setParent(south);
		Toolbarbutton cancel = new Toolbarbutton("",
				"/include/img/icon-back.png");
		cancel.setTooltiptext("Tutup");
		cancel.addEventListener("onClick", new EventListener() {
			@Override
			public void onEvent(Event event) throws Exception {
				addWindow.setVisible(false);
			}
		});
		cancel.setParent(toolbar);
		Toolbarbutton save = new Toolbarbutton("", "/include/img/icon-save.png");
		save.setTooltiptext("Simpan");
		save.addEventListener("onClick", new EventListener() {
			@Override
			public void onEvent(Event event) throws Exception {
				// if (onSave(event)) {
				// onSearchDefault(null);
				// addWindow.setVisible(false);
				// }
			}
		});
		save.setParent(toolbar);
		borderlayout.setParent(addWindow);
	}

	public void onAdd(Event event) throws Exception {
		init(new Berita());
		addWindow.setVisible(true);
		addWindow.onModal();
	}

	public boolean onSave(Event event) throws Exception {
		if (judul.getValue().trim().equals("")) {
			Messagebox.show("Judul harus diisi", "Peringatan", 1,
					Messagebox.INFORMATION);
			return false;
		}

		if (jenisBerita.getSelectedItem() == null) {
			Messagebox.show("Jenis Berita harus dipilih", "Peringatan", 1,
					Messagebox.INFORMATION);
			return false;
		}

		if (isi.getValue().trim().equals("")) {
			Messagebox.show("Isi harus diisi", "Peringatan", 1,
					Messagebox.INFORMATION);
			return false;
		}

		if (status.getSelectedItem() == null) {
			Messagebox.show("Status harus dipilih", "Peringatan", 1,
					Messagebox.INFORMATION);
			return false;
		}

		BeritaDao beritaDao = DaoFactory.getInstance().getBeritaDao();
		if (berita.getId() != null) {
			berita = beritaDao.load(berita.getId());
		}

		berita.setJudul(judul.getValue());
		berita.setJenisBerita((JenisBerita) jenisBerita.getSelectedItem() == null ? null
				: jenisBerita.getSelectedItem().getValue());

		return true;
	}
}
